
var T = null;
function test_init() {
	var T= new G.Test.TestSuit("TEST SUIT FOR Tree", "center");
	//T.stopOnError = true;

	/*
	 * Create a tree with the given level, and has the given mount of nest node
	 * at each level.
	 *
	 */
	var tree_build = function(lvl, leaf) 
	{
		var tree = new G.Utils.TreeNode('T', {id: 'T'});
		
		var curr = tree;
		for (lv = 0; lv < lvl; lv++) {
			// Create leaf nodes.
			for (var i = 0; i <= leaf; i++) {
				curr.append_child(new G.Utils.TreeNode(i, {id: i}));
			}
			curr = curr.childNodes[0];
		}
		return tree;
	}


	var tree_test_get_fullname = function() {
		var tree = new G.Utils.TreeNode('T', {id: 'T'});
		var n1 = new G.Utils.TreeNode('1', {id: '1'});
		var n2 = new G.Utils.TreeNode('2', {id: '2'});
		var n3 = new G.Utils.TreeNode('3', {id: '3'});
		tree.append_child(n1);
		n1.append_child(n2);
		n2.append_child(n3);

		this.assert_equ(tree.get_fullname(), "T");
		this.assert_equ(n1.get_fullname(), "T/1");
		this.assert_equ(n2.get_fullname(), "T/1/2");
		this.assert_equ(n3.get_fullname(), "T/1/2/3");
	}

	var tree_test_append = function() {
		var tree = tree_build(2, 2);
		var tree2 = tree_build(1, 1);
		this.assert_equ(tree.dump(), "T, T/0, T/0/0, T/0/1, T/0/2, T/1, T/2");
		this.assert_equ(tree2.dump(), "T, T/0, T/1");

		tree.append_child(tree2);
		this.assert_equ(tree.dump(), "T, T/0, T/0/0, T/0/1, T/0/2, T/1, T/2, T/T, T/T/0, T/T/1");
	}

	var tree_test_search = function() {
		var tree = tree_build(2, 2);
		// T is the name of tree's own name
		this.assert_equ(tree.search("T"), null);
		this.assert_equ(tree.search("0").get_fullname(), "T/0");
		this.assert_equ(tree.search("1").get_fullname(), "T/1");
		this.assert_equ(tree.search("2").get_fullname(), "T/2");
		this.assert_equ(tree.search("0/1").get_fullname(), "T/0/1");
		this.assert_equ(tree.search("0/2").get_fullname(), "T/0/2");
	}

	var tree_test_disconnect = function() {
		var tree = tree_build(2, 2);
		var n = tree.search("0");

		n.disconnect();

		this.assert_equ(n.dump(), "0, 0/0, 0/1, 0/2"); 
		this.assert_equ(tree.dump(), "T, T/1, T/2");

		n = tree.search("1");
		n.disconnect();
		this.assert_equ(n.dump(), "1");
		this.assert_equ(tree.dump(), "T, T/2");
	}

	var tree_test_move = function() {
		var tree = tree_build(2, 2);
		var n = tree.search("0");
		n.move(tree.search("1"));

		this.assert_equ(tree.dump(), "T, T/1, T/1/0, T/1/0/0, T/1/0/1, T/1/0/2, T/2"); 
	}

	T.add_case(new G.Test.TestCase('G.Tree.get_fullname', null, tree_test_get_fullname));
	T.add_case(new G.Test.TestCase('G.Tree.apend_child', null, tree_test_append));
	T.add_case(new G.Test.TestCase('G.Tree.search', null, tree_test_search));
	T.add_case(new G.Test.TestCase('G.Tree.disconnect', null, tree_test_disconnect));
	T.add_case(new G.Test.TestCase('G.Tree.move', null, tree_test_move));
	T.run();
}

